home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Cuadros de diálogo / HeadDump / HeadDump.cs next >
Encoding:
Text File  |  2002-05-23  |  2.6 KB  |  86 lines

  1. //---------------------------------------
  2. // HeadDump.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9. class HeadDump: Form
  10. {
  11.      const string strProgName  = "Volcado";
  12.      string       strFileName = "";
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new HeadDump());
  17.      }
  18.      public HeadDump()
  19.      {
  20.           Text = strProgName;
  21.           Font = new Font(FontFamily.GenericMonospace, 
  22.                           Font.SizeInPoints);
  23.  
  24.           Menu = new MainMenu();
  25.           Menu.MenuItems.Add("&Archivo");
  26.           Menu.MenuItems[0].MenuItems.Add("&Abrir...", 
  27.                               new EventHandler(MenuFileOpenOnClick));
  28.           Menu.MenuItems.Add("&Formato");
  29.           Menu.MenuItems[1].MenuItems.Add("&Fuente...",
  30.                               new EventHandler(MenuFormatFontOnClick));
  31.      }
  32.      void MenuFileOpenOnClick(object obj, EventArgs ea)
  33.      {
  34.           OpenFileDialog dlg = new OpenFileDialog();
  35.  
  36.           dlg.InitialDirectory = "c:\\" ;
  37.          
  38.           if (dlg.ShowDialog() == DialogResult.OK)
  39.           {
  40.                strFileName = dlg.FileName;
  41.                Text = strProgName + " - " + Path.GetFileName(strFileName);
  42.                Invalidate();
  43.           }
  44.      }
  45.      void MenuFormatFontOnClick(object obj, EventArgs ea)
  46.      {
  47.           FontDialog dlg = new FontDialog();
  48.  
  49.           dlg.Font = Font;
  50.           dlg.FixedPitchOnly = true;
  51.  
  52.           if (dlg.ShowDialog() == DialogResult.OK)
  53.           {
  54.                Font = dlg.Font;
  55.                Invalidate();
  56.           }
  57.      }
  58.      protected override void OnPaint(PaintEventArgs pea)
  59.      {
  60.           Graphics   grfx  = pea.Graphics;
  61.           Brush      brush = new SolidBrush(ForeColor);
  62.           FileStream fs;
  63.  
  64.           try
  65.           {
  66.                fs = new FileStream(strFileName, FileMode.Open,
  67.                                    FileAccess.Read, FileShare.Read);
  68.           }
  69.           catch
  70.           {
  71.                return;
  72.           }
  73.  
  74.           for (int iLine = 0; iLine <= ClientSize.Height / Font.Height; 
  75.                               iLine++)
  76.           {
  77.                byte[] abyBuffer = new byte[16];
  78.                int    iCount    = fs.Read(abyBuffer, 0, 16);
  79.                string str       = HexDump.ComposeLine(16 * iLine, 
  80.                                                       abyBuffer, iCount);
  81.  
  82.                grfx.DrawString(str, Font, brush, 0, iLine * Font.Height);
  83.           }
  84.           fs.Close();
  85.      }
  86.  }